package com.thb.pay.core.config;

import com.thb.pay.core.interceptor.GlobalLocaleInterceptor;
import com.thb.pay.core.interceptor.LogRequestProcessInterceptor;
import org.hibernate.validator.HibernateValidator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.validation.Validator;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;

import java.util.Locale;


public abstract class AbstractWebConfig extends WebMvcConfigurerAdapter {

    protected static final Logger logger = LoggerFactory.getLogger(AbstractWebConfig.class);

    @Autowired
    private MessageSource messageSource;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        preAddInterceptors(registry);
        registry.addInterceptor(logRequestProcessInterceptor()).addPathPatterns("/**");
        registry.addInterceptor(localeChangeInterceptor()).addPathPatterns("/**");
        registry.addInterceptor(globalLocaleInterceptor()).addPathPatterns("/**");

        addCustomInterceptors(registry);
        super.addInterceptors(registry);
    }

    /**
     * 后期在请求日志前面做一层 ip黑白名单防护等等
     *
     * @param registry
     */
    protected abstract void preAddInterceptors(InterceptorRegistry registry);

    protected abstract void addCustomInterceptors(InterceptorRegistry registry);

    @Bean
    public LocaleChangeInterceptor localeChangeInterceptor() {
        LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
        // 参数名
        //请求参数必须加上 post get url路径上必须拼接 lang来修改国际化标准 zh en.... and so on
        lci.setParamName("lang");
        return lci;
    }

    @Bean
    public LocaleResolver localeResolver() {
        SessionLocaleResolver slr = new SessionLocaleResolver();
        //设置默认区域,
        slr.setDefaultLocale(Locale.US);
        Locale.setDefault(Locale.US);
        logger.info("set default locale: {}", Locale.getDefault().getLanguage());
        return slr;
    }

    @Bean("mvcValidator")
    public Validator mvcValidator() {
        LocalValidatorFactoryBean localValidatorFactoryBean = new LocalValidatorFactoryBean();
        localValidatorFactoryBean.setProviderClass(HibernateValidator.class);
        localValidatorFactoryBean.setValidationMessageSource(messageSource);
        return localValidatorFactoryBean;
    }
    @Bean
    public LogRequestProcessInterceptor logRequestProcessInterceptor() {
        return new LogRequestProcessInterceptor();
    }

    @Bean
    public GlobalLocaleInterceptor globalLocaleInterceptor() {
        return new GlobalLocaleInterceptor();
    }
}
